package com.bh.vehicle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.annotation.DataDictClass;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.vehicle.domain.JcCarrier;
import com.bh.vehicle.mapper.JcCarrierMapper;
import com.bh.vehicle.service.IJcCarrierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 承运商Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcCarrierServiceImpl extends ServiceImpl<JcCarrierMapper, JcCarrier> implements IJcCarrierService {
    @Autowired
    private JcCarrierMapper jcCarrierMapper;

    /**
     * 查询承运商
     *
     * @param id 承运商主键
     * @return 承运商
     */
    @DataDictClass
    @Override
    public JcCarrier selectJcCarrierById(Long id) {
        return jcCarrierMapper.selectById(id);
    }

    /**
     * 查询承运商列表
     *
     * @param jcCarrier 承运商
     * @return 承运商
     */
    @DataDictClass
    @Override
    public List<JcCarrier> selectJcCarrierList(JcCarrier jcCarrier) {
        return jcCarrierMapper.selectJcCarrierList(jcCarrier);
    }

    /**
     * 新增承运商
     *
     * @param jcCarrier 承运商
     * @return 结果
     */
    @Override
    public int insertJcCarrier(JcCarrier jcCarrier) {
        if (jcCarrierMapper.selectCodeUnique(jcCarrier.getCode()) != null) {
            throw new ServiceException("承运商编码已存在");
        }
        jcCarrier.setJcOrganizationId(SecurityUtils.getOrgId());
        jcCarrier.setCreateTime(DateUtils.getNowDate());
        jcCarrier.setCreateName(SecurityUtils.getUsername());
        return jcCarrierMapper.insert(jcCarrier);
    }

    /**
     * 修改承运商
     *
     * @param jcCarrier 承运商
     * @return 结果
     */
    @Override
    public int updateJcCarrier(JcCarrier jcCarrier) {
        JcCarrier carrier = jcCarrierMapper.selectCodeUnique(jcCarrier.getCode());
        if (carrier != null && !carrier.getId().equals(jcCarrier.getId())) {
            throw new ServiceException("承运商编码已存在");
        }
        jcCarrier.setModifyTime(DateUtils.getNowDate());
        jcCarrier.setModifyName(SecurityUtils.getUsername());
        return jcCarrierMapper.updateById(jcCarrier);
    }

    /**
     * 批量删除承运商
     *
     * @param ids 需要删除的承运商主键
     * @return 结果
     */
    @Override
    public int deleteJcCarrierByIds(Long[] ids) {
        return jcCarrierMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除承运商信息
     *
     * @param id 承运商主键
     * @return 结果
     */
    @Override
    public int deleteJcCarrierById(Long id) {
        return jcCarrierMapper.deleteById(id);
    }
}
